﻿// admin2.cpp: 实现文件
//

#include "pch.h"
#include "luntan.h"
#include "admin2.h"
#include "afxdialogex.h"
#include"admin.h"


// admin2 对话框

IMPLEMENT_DYNAMIC(admin2, CDialogEx)

admin2::admin2(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_ADMIN2, pParent)
	, m_boardid(_T(""))
	, m_boardname(_T(""))
	, m_boardcontent(_T(""))
	, m_boardname2(_T(""))
	, m_boardcent(_T(""))
	, m_bname(_T(""))
{

}

admin2::~admin2()
{
}

void admin2::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_LIST1, m_list3);
	//  DDX_Text(pDX, IDC_EDIT1, m_board);
	DDX_Text(pDX, IDC_EDIT1, m_boardid);
	DDX_Text(pDX, IDC_EDIT2, m_boardname);
	DDX_Text(pDX, IDC_EDIT3, m_boardcontent);
	DDX_Text(pDX, IDC_EDIT6, m_boardname2);
	DDX_Text(pDX, IDC_EDIT5, m_boardcent);
	DDX_Text(pDX, IDC_EDIT4, m_bname);
}


BEGIN_MESSAGE_MAP(admin2, CDialogEx)
	ON_BN_CLICKED(IDC_BUTTON8, &admin2::OnBnClickedButton8)
	ON_BN_CLICKED(IDC_BUTTON7, &admin2::OnBnClickedButton7)
	ON_BN_CLICKED(IDC_BUTTON12, &admin2::OnBnClickedButton12)
	ON_NOTIFY(NM_CLICK, IDC_LIST1, &admin2::OnNMClickList1)
	ON_BN_CLICKED(IDC_BUTTON13, &admin2::OnBnClickedButton13)
	ON_BN_CLICKED(IDC_BUTTON1, &admin2::OnBnClickedButton1)
	ON_BN_CLICKED(IDC_BUTTON2, &admin2::OnBnClickedButton2)
END_MESSAGE_MAP()


// admin2 消息处理程序


void admin2::OnBnClickedButton8()
{
	// TODO: 在此添加控件通知处理程序代码
	admin2::OnOK();
	admin a;
	a.DoModal();
}


BOOL admin2::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// TODO:  在此添加额外的初始化
	m_list3.SetExtendedStyle(LVS_EX_FLATSB //扁平风格显示滚动条
		| LVS_EX_FULLROWSELECT //允许整行选中
		| LVS_EX_HEADERDRAGDROP //允许整列拖动
		| LVS_EX_ONECLICKACTIVATE //单击选中项
		| LVS_EX_GRIDLINES); //画出网格线

	//设置表头
	m_list3.InsertColumn(0, _T("版块ID"), LVCFMT_LEFT, 100, 25);
	m_list3.InsertColumn(3, _T("版块管理员ID"), LVCFMT_LEFT, 100, 25);
	m_list3.InsertColumn(1, _T("版块名"), LVCFMT_LEFT, 100, 25);
	m_list3.InsertColumn(2, _T("版块简介"), LVCFMT_LEFT, 250, 25);
	
	AddToGrid();
	return TRUE;  // return TRUE unless you set the focus to a control
				  // 异常: OCX 属性页应返回 FALSE
}


void admin2::OnBnClickedButton7()
{
	// TODO: 在此添加控件通知处理程序代码
	UpdateData(true);
	if (m_boardid != "" && m_boardname != "" && m_boardcontent != "")
	{
		ADOConn m_AdoConn;
		m_AdoConn.OnInitADOConn();
		CString sql1, sql2;
		sql1 = "select count(Board_id) from Board";
		m_AdoConn.m_pRecordset = m_AdoConn.GetRecordSet((_bstr_t)sql1);
		m_bid = m_AdoConn.m_pRecordset->GetCollect("");
		m_bid.Format("%04d", atoi(m_bid) + 1);

		sql2.Format("insert into Board(Board_id,Admin_id,Board_name,Board_content) values('%s','%s','%s','%s')", \
			m_bid, m_boardid, m_boardname, m_boardcontent);
		m_AdoConn.ExecuteSQL((_bstr_t)sql2);
		//MessageBox("发布成功","发布结果");
		m_list3.DeleteAllItems();
		m_boardid = "", m_boardname = "", m_boardcontent = "";
		UpdateData(FALSE);
		m_list3.DeleteAllItems();
		AddToGrid(); //向列表视图控件中插入数据
		m_bid = "";
	}
	else
	{
		MessageBox("添加的版块信息不能为空！", "提示", MB_ICONWARNING);
	}
	
	
}


void admin2::AddToGrid()
{
	ADOConn m_AdoConn; //ADOConn对象
	m_AdoConn.OnInitADOConn(); //连接数据库
	CString sql;
	int i = 0;
	sql.Format("select Board_id,Board_name,Board_content,Admin_id from Board");
	m_AdoConn.m_pRecordset = m_AdoConn.GetRecordSet((_bstr_t)sql); //根据SQL语句进行查询
	//MessageBox((_bstr_t)m_AdoConn.m_pRecordset->GetCollect("User_Name"));
	while (!m_AdoConn.m_pRecordset->adoEOF)
	{
		m_list3.InsertItem(i, ""); //向列表视图控件中插入行
		//向列表视图控件中插入列
		m_list3.SetItemText(i, 0, (_bstr_t)m_AdoConn.m_pRecordset->GetCollect("Board_id"));
		m_list3.SetItemText(i, 1, (_bstr_t)m_AdoConn.m_pRecordset->GetCollect("Board_name"));
		m_list3.SetItemText(i, 2, (_bstr_t)m_AdoConn.m_pRecordset->GetCollect("Board_content"));
		m_list3.SetItemText(i, 3, (_bstr_t)m_AdoConn.m_pRecordset->GetCollect("Admin_id"));
		m_AdoConn.m_pRecordset->MoveNext(); //将记录集指针移动到下一条记录
		i++;
	}
	m_AdoConn.ExitConnect();
}


void admin2::OnBnClickedButton12()
{
	UpdateData(TRUE);
	if (m_boardname2 != "" && m_boardcent != "")
	{
		if (m_bid != "")
		{
			ADOConn m_AdoConn; //ADOConn对象
			m_AdoConn.OnInitADOConn(); //连接数据库
			CString sql;
			sql.Format("update Board set Board_name='%s',Board_content='%s' where Board_id='%s'", m_boardname2, m_boardcent, m_bid);
			m_AdoConn.ExecuteSQL((_bstr_t)sql);
			m_AdoConn.ExitConnect();
			m_boardname2 = "", m_boardcent = "";
			UpdateData(FALSE);
			m_list3.DeleteAllItems();
			AddToGrid(); //向列表视图控件中插入数据
			m_bid = "";
		}
		else
		{
			MessageBox("请选择所要更改的版块！", "提示", MB_ICONWARNING);
		}	
	}
	else
	{
		MessageBox("所修改的版块信息不能为空！", "提示", MB_ICONWARNING);
	}
}


void admin2::OnNMClickList1(NMHDR* pNMHDR, LRESULT* pResult)
{
	LPNMITEMACTIVATE pNMItemActivate = reinterpret_cast<LPNMITEMACTIVATE>(pNMHDR);
	// TODO: 在此添加控件通知处理程序代码
	int pos = m_list3.GetSelectionMark();
	m_bid = m_list3.GetItemText(pos, 0);
	*pResult = 0;
}


void admin2::OnBnClickedButton13()
{
	
	if (m_bid != "")
	{
		ADOConn m_AdoConn; //ADOConn对象
		m_AdoConn.OnInitADOConn(); //连接数据库
		CString sql;
		sql.Format("delete from Board where Board_id='%s'", m_bid);
		m_AdoConn.ExecuteSQL((_bstr_t)sql);
		m_AdoConn.ExitConnect();
		m_list3.DeleteAllItems();
		AddToGrid(); //向列表视图控件中插入数据
		m_bid = "";
	}
	else
	{
		MessageBox("请选择要删除的版块！", "提示", MB_ICONWARNING);
	}
}


void admin2::OnBnClickedButton1()
{
	// TODO: 在此添加控件通知处理程序代码
	m_list3.DeleteAllItems();
	AddToGrid(); //向列表视图控件中插入数据
}


void admin2::OnBnClickedButton2()
{
	
	UpdateData(TRUE);
	if (m_bname!= "")
	{
		m_list3.DeleteAllItems();
		ADOConn m_AdoConn;
		m_AdoConn.OnInitADOConn();
		CString sql;
		int i = 0;
		sql.Format("select Board_id,Board_name,Board_content,Admin_id from Board where Board_name like '%s%s%s'", \
			"%", m_bname, "%"); //设置SQL语句
		m_AdoConn.m_pRecordset = m_AdoConn.GetRecordSet((_bstr_t)sql); //根据SQL语句进行查询
		while (!m_AdoConn.m_pRecordset->adoEOF)
		{
			m_list3.InsertItem(i, ""); //向列表视图控件中插入行
		//向列表视图控件中插入列
			m_list3.SetItemText(i, 0, (_bstr_t)m_AdoConn.m_pRecordset->GetCollect("Board_id"));
			m_list3.SetItemText(i, 1, (_bstr_t)m_AdoConn.m_pRecordset->GetCollect("Board_name"));
			m_list3.SetItemText(i, 2, (_bstr_t)m_AdoConn.m_pRecordset->GetCollect("Board_content"));
			m_list3.SetItemText(i, 3, (_bstr_t)m_AdoConn.m_pRecordset->GetCollect("Admin_id"));
			m_AdoConn.m_pRecordset->MoveNext(); //将记录集指针移动到下一条记录
			i++;
		}
		m_AdoConn.ExitConnect();
		m_bname = "";
		UpdateData(false);
	}
	else
	{
		MessageBox("查询条件不能为空！", "提示", MB_ICONWARNING);
	}

}
